﻿/******************************* 
** 作者： 斯泰尔 
** 时间： 2021-08-11 13:34:41 
** 版本: V1.0.0 
** CLR: 4.0.30319.42000 
** GUID: 0019bd7a-0add-4208-bced-a913d652941f 
** 描述： 尚未编写描述 
*******************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;

namespace AliSMS
{
    class AppCode
    {
        InitFile init = new InitFile();
        AliSMS Send = new AliSMS();
        MsSqlHelper mssql = new MsSqlHelper();

       public DataTable DataSource(DataRow dr)
        {
            DataTable dt = new DataTable();
            try
            {
                string SqlStr = dr["QuerySQL"].ToString();
                dt = mssql.ExecuteDt(SqlStr);
            }
            catch (Exception ex)
            {
                init.WriteLog(ex.ToString(), true);
            }
            return dt;
        }

        public bool SendData(DataRow dr)
        {
            bool flag = false;
            try
            {
                string TemplateCode = dr["TemplateCode"].ToString();
                DataTable dt=DataSource(dr);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string number = dt.Rows[i][1].ToString();
                   // init.WriteLog(number, false);

                    DataTable dt1 = dt.Clone();
                    dt1.Rows.Add(dt.Rows[i].ItemArray);                        

                    if (dt1.Columns.Contains("tel")){dt1.Columns.Remove("tel");}
                    
                    object[] param = dt1.Rows[0].ItemArray;

                    string sendnote = string.Format(dr["TemplateSMS"].ToString(), param);
                    string ret = Send.SendPost(number, sendnote, TemplateCode);
                    string result =ret+"|"+ "{\"phonenumber\":\"" + number + "\",\"SMS\":[" + sendnote + "]}";
                    if (ret == "OK")
                    {                        
                        string updatesql = string.Format(dr["UpdateSQL"].ToString(), dt.Rows[i][0].ToString());
                        if (mssql.ExecuteSql(updatesql) > 0)
                        {                            
                            init.WriteLog(result, false);
                        }
                        else
                        {
                            init.WriteLog("Error:Update|"+result, false);
                        }
                    }
                    else
                    {
                        init.WriteLog(result, true);
                    }
                    Thread.Sleep(3000);
                }
                flag = true;
            }
            catch (Exception ex)
            {
                init.WriteLog(ex.ToString(), true);
                flag = false;
            }
            return flag;
        }
    }
}
